<template>
  <el-dialog :visible.sync="visible" top="20px" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="140px">
      <el-form-item prop="orgName" :label="$t('lawOffice.orgName')">
        <el-input v-model="dataForm.orgName" :placeholder="$t('lawOffice.orgName')"/>
      </el-form-item>
      <el-form-item prop="unifiedSocialCreditCode" :label="$t('lawOffice.unifiedSocialCreditCode')">
        <el-input v-model="dataForm.unifiedSocialCreditCode" :placeholder="$t('lawOffice.unifiedSocialCreditCode')"/>
      </el-form-item>
      <el-form-item prop="gxsfj" :label="$t('lawOffice.gxsfj')">
        <el-input v-model="dataForm.gxsfj" :placeholder="$t('lawOffice.gxsfj')" />
      </el-form-item>
      <el-form-item prop="zyxkz" :label="$t('lawOffice.zyxkz')">
        <el-input v-model="dataForm.zyxkz" :placeholder="$t('lawOffice.zyxkz')" />
      </el-form-item>
      <el-form-item prop="registerDate" :label="$t('lawOffice.registerDate')">
        <el-date-picker v-model="dataForm.registerDate" type="date" :placeholder="$t('lawOffice.registerDate')" />
      </el-form-item>
      <el-form-item prop="leader" :label="$t('lawOffice.leader')">
        <el-input v-model="dataForm.leader" :placeholder="$t('lawOffice.leader')" />
      </el-form-item>
      <el-form-item prop="phone" :label="$t('lawOffice.phone')">
        <el-input v-model="dataForm.phone" :placeholder="$t('lawOffice.phone')" />
      </el-form-item>
      <el-form-item prop="address" :label="$t('lawOffice.address')">
        <el-input v-model="dataForm.address" :placeholder="$t('lawOffice.address')" />
      </el-form-item>
      <el-form-item prop="postalCode" :label="$t('lawOffice.postalCode')">
        <el-input v-model="dataForm.postalCode" :placeholder="$t('lawOffice.postalCode')" />
      </el-form-item>
      <el-form-item prop="remark" :label="$t('remark')">
        <el-input v-model="dataForm.remark" :placeholder="$t('remark')"/>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { debounce } from 'lodash'
export default {
  data () {
    return {
      visible: false,
      menuList: [],
      deptList: [],
      dataForm: {
        id: '',
        orgName: '',
        unifiedSocialCreditCode: '',
        gxsfj: '',
        zyxkz: '',
        registerDate: '',
        leader: '',
        phone: '',
        address: '',
        postalCode: '',
        remark: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        orgName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        Promise.all([

        ]).then(() => {
          if (this.dataForm.id) {
            this.getInfo()
          }
        })
      })
    },
    // 获取信息
    getInfo () {
      this.$axios.get(`/lawOffice/info/${this.dataForm.id}`).then(res => {
        this.dataForm = {
          ...this.dataForm,
          ...res
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        // this.dataForm.menuIdList = JSON.stringify(this.dataForm.menuIdList)
        this.$http.json('/lawOffice/addOrUpdate', this.dataForm).then(res => {
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
